package com.item;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.TreeMap;

/**
 * 由键决定特性：不重复、无索引、可排序（默认键的大小升序排序）
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        test2();
    }
    public static void test2(){
        TreeMap<Student,String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()-o2.getAge();
            }
        });
        treeMap.put(new Student("小王", 18), "北京");
        treeMap.put(new Student("小李", 20), "上海");
        treeMap.put(new Student("小王", 19), "广州");
        treeMap.put(new Student("小赵", 22), "深圳");
        treeMap.put(new Student("小钱", 21), "重庆");
        treeMap.forEach((k,v)-> System.out.println(k+":"+v));
    }

    @Test
    public void test1() {
        TreeMap<Student,String> treeMap = new TreeMap<>();
        treeMap.put(new Student("小王", 18), "北京");
        treeMap.put(new Student("小李", 20), "上海");
        treeMap.put(new Student("小王", 19), "广州");
        treeMap.put(new Student("小赵", 22), "深圳");
        treeMap.put(new Student("小钱", 21), "重庆");
        treeMap.forEach((k,v)-> System.out.println(k+":"+v));


    }
}
